namespace DotNetNuke.Services.Scheduling
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Framework;
    using DotNetNuke.Framework.Providers;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;

    public abstract class SchedulingProvider
    {
        private static bool _Debug;
        private static int _MaxThreads;
        private ProviderConfiguration _providerConfiguration = ProviderConfiguration.GetProviderConfiguration("scheduling");
        private string _providerPath;
        public DotNetNuke.Services.Scheduling.EventName EventName;
        private static SchedulingProvider objProvider = null;

        static SchedulingProvider()
        {
            CreateProvider();
        }

        public SchedulingProvider()
        {
            Provider objProvider = (Provider) this._providerConfiguration.Providers[this._providerConfiguration.DefaultProvider];
            this._providerPath = objProvider.Attributes["providerPath"];
            if (objProvider.Attributes["debug"] != null)
            {
                _Debug = Convert.ToBoolean(objProvider.Attributes["debug"]);
            }
            if (objProvider.Attributes["maxThreads"] != null)
            {
                _MaxThreads = Convert.ToInt32(objProvider.Attributes["maxThreads"]);
            }
            else
            {
                _MaxThreads = 1;
            }
        }

        public abstract int AddSchedule(ScheduleItem objScheduleItem);
        private static void CreateProvider()
        {
            objProvider = (SchedulingProvider) Reflection.CreateObject("scheduling");
        }

        public abstract void DeleteSchedule(ScheduleItem objScheduleItem);
        public abstract void ExecuteTasks();
        public abstract int GetActiveThreadCount();
        public abstract int GetFreeThreadCount();
        public abstract int GetMaxThreadCount();
        public abstract ScheduleItem GetNextScheduledTask(string Server);
        public abstract string GetProviderPath();
        public abstract ArrayList GetSchedule();
        public abstract ScheduleItem GetSchedule(int ScheduleID);
        public abstract ArrayList GetSchedule(string Server);
        public abstract ScheduleItem GetSchedule(string TypeFullName, string Server);
        public abstract ArrayList GetScheduleHistory(int ScheduleID);
        public abstract Hashtable GetScheduleItemSettings(int ScheduleID);
        public abstract Collection GetScheduleProcessing();
        public abstract Collection GetScheduleQueue();
        public abstract ScheduleStatus GetScheduleStatus();
        public abstract void Halt(string SourceOfHalt);
        public static SchedulingProvider Instance()
        {
            return objProvider;
        }

        public abstract void PurgeScheduleHistory();
        public abstract void ReStart(string SourceOfRestart);
        public abstract void RunEventSchedule(DotNetNuke.Services.Scheduling.EventName objEventName);
        public abstract void Start();
        public abstract void StartAndWaitForResponse();
        public abstract void UpdateSchedule(ScheduleItem objScheduleItem);

        public static bool Debug
        {
            get
            {
                return _Debug;
            }
        }

        public static bool Enabled
        {
            get
            {
                return (SchedulerMode != DotNetNuke.Services.Scheduling.SchedulerMode.DISABLED);
            }
        }

        public static int MaxThreads
        {
            get
            {
                return _MaxThreads;
            }
        }

        public string ProviderPath
        {
            get
            {
                return this._providerPath;
            }
        }

        public static bool ReadyForPoll
        {
            get
            {
                return (DataCache.GetCache("ScheduleLastPolled") == null);
            }
        }

        public static DateTime ScheduleLastPolled
        {
            get
            {
                if (DataCache.GetCache("ScheduleLastPolled") != null)
                {
                    return Convert.ToDateTime(RuntimeHelpers.GetObjectValue(DataCache.GetCache("ScheduleLastPolled")));
                }
                return DateTime.MinValue;
            }
            set
            {
                DateTime ns;
                ScheduleItem s = Instance().GetNextScheduledTask(DotNetNuke.Common.Globals.ServerName);
                if (s != null)
                {
                    DateTime NextStart = s.NextStart;
                    if (DateTime.Compare(NextStart, DateAndTime.Now) >= 0)
                    {
                        ns = NextStart;
                    }
                    else
                    {
                        ns = DateAndTime.Now.AddMinutes(1.0);
                    }
                }
                else
                {
                    ns = DateAndTime.Now.AddMinutes(1.0);
                }
                DataCache.SetCache("ScheduleLastPolled", value, ns);
            }
        }

        public static DotNetNuke.Services.Scheduling.SchedulerMode SchedulerMode
        {
            get
            {
                if (Convert.ToString(RuntimeHelpers.GetObjectValue(DotNetNuke.Common.Globals.HostSettings["SchedulerMode"])).Length == 0)
                {
                    return DotNetNuke.Services.Scheduling.SchedulerMode.TIMER_METHOD;
                }
                return (DotNetNuke.Services.Scheduling.SchedulerMode) Conversions.ToInteger(DotNetNuke.Common.Globals.HostSettings["SchedulerMode"]);
            }
        }
    }
}

